package DAO;

import Bean.Book;

import java.util.List;

/**
 * @author jianjun
 * @Describe
 * @creat 2021-12-22-21:27
 */
public class BookDAOImpl extends BaseDAO implements BookDAO {

    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book(`name`,`price`,`author`,`sales`,`stock`,`IMG_path`) value(?,?,?,?,?,?)";
        return updata(sql, book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getIMG_path());
    }

    @Override
    public int deleteBook(Integer id) {
        String sql = "delete from t_book where id = ?";
        return updata(sql, id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set`name`=?,`price`=?,`author`=?,`sales`=?,`stock`=?,`IMG_path`=? where id = ?";
        return updata(sql, book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getIMG_path(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql = "select * from t_book where id =?";
        return queryForOne(Book.class,sql, id);
    }

    @Override
    public List<Book> query() {
        String sql = "select * from t_book";
        return queryForList(Book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_book";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public Integer queryForPageTotalCount(Integer minPrice, Integer maxPrice) {
        String sql = "select count(*) from t_book where price between ? and ?";
        Number count = (Number) queryForSingleValue(sql,minPrice,maxPrice);
        return count.intValue();
    }

    @Override
    public List<Book> queryForItems(Integer pageBegin, Integer pageSize) {
        String sql = "select * from t_book limit ?,?";
        return queryForList(Book.class,sql,pageBegin,pageSize);
    }

    @Override
    public List<Book> queryForItems(Integer pageBegin, Integer pageSize, Integer minPrice, Integer maxPrice) {
        String sql = "select * from t_book where price between ? and ? order by price limit ?,?";
        return queryForList(Book.class,sql,minPrice,maxPrice,pageBegin,pageSize);
    }
}
